<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>

        .ht {
            text-align: center;
            color: cadetblue;
            font-family: 幼圆, serif;
        }

        .tab {
            width: 500px;
            border: 5px solid cadetblue;
            margin: 0 auto;
            border-radius: 5px;
            font-family: 幼圆, serif;
        }

        .ltr td {
            border: 1px solid powderblue;

        }

        .ipt {
            border: 0;
            width: 50%;

        }

        .btn1 {
            border: 2px solid powderblue;
            border-radius: 4px;
            width: 60px;
            background-color: antiquewhite;

        }

        .msg {
            color: gold;
        }

        .buttonContainer {
            text-align: center;
        }
    </style>
    <title>日程管理系统注册页</title>

    <script>
        function checkUsername() {
            var elementById = document.getElementById("usernameInput");
            var value = elementById.value;
            var elementById1 = document.getElementById("usernameMsg");
            var usernameReg = /^[a-zA-Z ][a-zA-Z-0-9]{5,9}$/;
            if (!usernameReg.test(value)) {
                elementById1.innerText = "账号格式错误！"
                return false
            }

            var xmlHttpRequest = new XMLHttpRequest();
            xmlHttpRequest.onreadystatechange = function () {
                if (xmlHttpRequest.readyState === 4 && xmlHttpRequest.status === 200) {
                    var parse = JSON.parse(xmlHttpRequest.responseText);
                    if (parse.code === 200) {
                        elementById1.innerText = "OK"
                        elementById1.style.color = "green"
                        return true
                    } else {
                        elementById1.innerText = "用户名已被占用"
                        elementById1.style.color = "red"
                        return false
                    }
                }
            }
            xmlHttpRequest.open("GET", "/user/checkUserUsed?username=" + value);
            xmlHttpRequest.send();
        }

        function checkUserPwd() {
            var elementById = document.getElementById("userPwdInput");
            var value = elementById.value;
            var elementById1 = document.getElementById("userPwdMsg");
            var passwordReg = /^[a-zA-Z0-9 _\-@#&*]{6,12}$/;
            if (!passwordReg.test(value)) {
                elementById1.innerText = "密码格式错误！"
                return false
            } else {
                elementById1.innerText = "密码格式正确！"
                elementById1.style.color = "green"
                return true
            }
        }

        function checkReUserPwd() {
            var elementById = document.getElementById("userPwdInput");
            var value = elementById.value;
            var elementById1 = document.getElementById("reUserPwdInput");
            var confirm = elementById1.value;
            var elementById2 = document.getElementById("reUserPwdMsg");
            if (value === confirm) {
                elementById2.innerText = "OK"
                elementById2.style.color = "green"
                return true
            } else {
                elementById2.innerText = "密码不一致"
                elementById2.style.color = "red"
                return false
            }
        }

        function checkForm() {
            var b = checkUsername();
            var b1 = checkUserPwd();
            var b2 = checkReUserPwd();

            return b && b1 && b2;
        }
    </script>

</head>
<body>
<h1 class="ht">欢迎使用日程管理系统</h1>
<h3 class="ht">请注册</h3>
<form method="post" action="/user/regist" onsubmit="return checkForm()">
    <table class="tab">
        <tr class="ltr">
            <td>请输入账号</td>
            <td>
                <input class="ipt" id="usernameInput" type="text" name="username" onblur="checkUsername()">
                <span id="usernameMsg" class="msg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td>请输入密码</td>
            <td>
                <input class="ipt" id="userPwdInput" type="password" name="userPwd" onblur="checkUserPwd()">
                <span id="userPwdMsg" class="msg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td>确认密码</td>
            <td>
                <input class="ipt" id="reUserPwdInput" type="password" onblur="checkReUserPwd()">
                <span id="reUserPwdMsg" class="msg"></span>
            </td>
        </tr>
        <tr class="ltr">
            <td colspan="2" class="buttonContainer">
                <input class="btn1" type="submit" value="注册">
                <input class="btn1" type="reset" value="重置">
                <button class="btn1"><a href="login.html">去登录</a></button>
            </td>
        </tr>
    </table>

</form>
</body>
</html>